import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * A Servlet to help view jpg images
 *
 * @author James Chy
 */

public class MaintainPlaces extends PhotoServlet {

    String initpath;
    String subtitle, table, thispage, code, codedesc;
    PrintWriter out;

    static final long serialVersionUID = 0;
    
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        subtitle = "Places";
        table="places";
        thispage="MaintainPlaces";
        code="place";
        codedesc="Place";
    }
        

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

        out.println("<title>Maintain " + subtitle + "</title>");  
        out.println("</head>");
        out.println("<body>");

        try
        {
            String query;
            ResultSet rs;

            out.println("<FORM ACTION=\"" + thispage + "\" METHOD=\"POST\">");

            query = "SELECT uid, " + code + " " +
                    "FROM " + table + " " +
                    "ORDER BY " + code;
            
            if(showQuery)
                out.println("Query:<BR>" + query + "<BR>");

            rs = executeQuery(query);

            out.println("<B>" + subtitle + "</B><BR>");
            out.println("<SELECT NAME=\"selection\" SIZE=\"20\">");
            while (rs.next()) 
            {
                String uid = rs.getString("uid");
                String value = rs.getString(code);    
                out.print("<OPTION ");
                out.println("VALUE=\"" + uid + "\">" + value);
            }
            out.println("</SELECT>");

            out.println("<BR><BR>");
            out.println(codedesc + "  <INPUT TYPE=TEXT NAME=TEXTINPUT><BR><BR>");
            out.println("<INPUT TYPE=SUBMIT NAME=\"Add\" VALUE=\"Add\">");
            out.println("Show Query <INPUT TYPE=CHECKBOX NAME=\"showQuery\">");
            out.println("</FORM>");

        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception");
        }
        
        out.println("</body>");
        out.println("</html>");

    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();
        
        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

		out.println("<title>Post</title>");  
        out.println("</head>");
        out.println("<body>");

        if(showQuery)
        {
            for(Enumeration e=request.getParameterNames(); e.hasMoreElements(); )
            {
                String currentElement=e.nextElement().toString();
                String currentValues[]=request.getParameterValues(currentElement);
                for(int i=0; i<currentValues.length; i++)
                    out.println(currentElement + "[" + i + "]=" + 
                            currentValues[i] + "<BR>");
            }
        }

        String query;

        String value=request.getParameter("TEXTINPUT");

        query = "INSERT INTO " + table + " " +
                "SET " + code + "=\"" + value + "\"";

        if(showQuery) out.println("Query: <BR>" + query + "<BR>");

        int count = executeUpdate(query);
        if(count > 0)
        {
           out.println("<H1>Data Saved</H1>");
        }
        else
        {
           out.println("<H1>Error: Nothing Saved</H1>");
        }

        out.println("<A HREF=" + thispage + "><H3>Return to Previous Page</H3></A>");

        out.println("</BODY></HTML>");
    }
               
}

